﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace MagnetExtractor
{
    public static class ResourcesAccess
    {
        public static void CopyEmbededResourceOnDisk(string resource, string path)
        {
            if (!File.Exists(System.IO.Path.Combine(path, resource)))
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                int count = 0;
                byte[] buffer = new byte[0x800];
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                using (Stream stream = executingAssembly.GetManifestResourceStream(string.Format("{0}.{1}.{2}", executingAssembly.GetName().Name, "External", resource)))
                {
                    using (FileStream stream2 = new FileStream(path + @"\" + resource, FileMode.Create, FileAccess.Write))
                    {
                        using (BinaryWriter writer = new BinaryWriter(stream2))
                        {
                            while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                writer.Write(buffer, 0, count);
                                writer.Flush();
                            }
                        }
                    }
                }
                buffer = null;
            }
        }
    }
}
